﻿using MoetutuServer.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using static MoetutuServer.WebSite.Pixiv.PixivHelper;

namespace MoetutuServer.WebSite.Pixiv
{
    class TagHelper
    {
        /// <summary>
        /// 处理tag标签，告诉用户已经汉化
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static List<String> TranslateTagForText(List<String> JpTag)
        {
            List<String> TempTag = new List<string>();
            foreach (String line in JpTag)
            {

                String GetTag = SearchCnTag(line);

                if (GetTag != "")
                {
                    TempTag.Add(line);
                    TempTag.Add(line + string.Format("(已汉化：{0})", GetTag));
                    continue;
                }
                else
                {
                    TempTag.Add(line);
                    FileHelper.AppendUTF8Text(".\\english.txt", line);

                }

            }
            return TempTag;
        }
        /// <summary>
        /// 删除掉已经有汉化的标签。
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static List<string> RemoveCnTag(List<string> JpTag)
        {

            List<String> TempTag = new List<string>();
            foreach (String line in JpTag)
            {

                String GetTag = SearchCnTag(line);

                if (GetTag != "")
                {

                    continue;
                }
                else
                {
                    TempTag.Add(line);


                }

            }
            return TempTag;
        }
        /// <summary>
        /// 翻译标签
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static List<String> TranslateTag(List<String> JpTag)
        {
            List<String> TempTag = new List<string>();

            for (int i = 0; i < JpTag.Count; i++) {
                String Tag = MoetutuServer.Pixiv.ProcessImg.Helper.TagShort(JpTag[i]);//先进行一次缩短
                String GetTag = SearchCnTag(Tag);//在进行汉化

                if (GetTag != "")
                {
                    if (!TempTag.Contains(GetTag))
                    {
                        Console.WriteLine("已汉化" + Tag);
                        TempTag.Add(GetTag);
                       
                    }
                }
                else
                {
                
                    FileHelper.AppendUTF8Text(".\\english.txt", Tag);

                }
                if (!TempTag.Contains(Tag))
                    TempTag.Add(Tag);
            }

            foreach (string line in TempTag) {
                Console.WriteLine("已处理过："+line);
            }
            return TempTag;
        }

        /// <summary>
        /// 读取xml信息方便汉化
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static List<TagStruct> JpTagReadAll(String FileName)
        {
            List<TagStruct> TagList = new List<TagStruct>();

            string Shtml = FileHelper.ReadContextUtf8(StaticValue.XmlPath);
          
            foreach (string line in HtmlHelper.GetHtmlValueList(Shtml, "//tag"))
            {
              
                TagStruct TagTemp = new TagStruct();

                TagTemp.JpTag1 = HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(line, "//jptag")).Trim();
                TagTemp.CnTag1 = HtmlHelper.GetFirstValue(HtmlHelper.GetTextValueList(line, "//cntag")).Trim();
                TagList.Add(TagTemp);

            }

            return TagList;
        }
        /// <summary>
        /// 读取jp的子列表
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public static List<String> XMLReadJp(List<TagStruct> TagStruct)
        {
            List<String> JpList = new List<string>();
            foreach (TagStruct Single in TagStruct)
            {

                JpList.Add(Single.JpTag1);
            }
            return JpList;

        }
        /// <summary>
        /// 查询中文标签
        /// </summary>
        /// <param name="JpTag"></param>
        /// <returns></returns>
        public static string SearchCnTag(String JpTag)
        {
            String text = "";
            IEnumerable<TagStruct> tagStruct = StaticValue.TagStruct.Where(x => x.JpTag1 == JpTag.Trim());
            foreach (TagStruct single in tagStruct)
            {
                text = single.CnTag1;
            }
            return text;
        }
        /// <summary>
        /// 添加翻译进去
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="JpTagText"></param>
        /// <param name="CnTagText"></param>
        public static void XmlAdd(String FileName, String JpTagText, String CnTagText)
        {
            //
            string TagMode = "<tag><jptag>{0}</jptag><cntag>{1}</cntag></tag>";
            FileHelper.AppendUTF8Text(FileName, string.Format(TagMode, JpTagText, CnTagText));
         //   xmlDoc.Save(FileName);
        }
    }
}
